package customer.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.tomcat.dbcp.dbcp.BasicDataSource;



public class GetCustomerByIdServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//ID로 고객정보를 조회
		response.setContentType("text/html;charset=UTF-8");
		PrintWriter out = response.getWriter();
		
		String id = request.getParameter("id");
		ServletContext ctx = getServletContext();
		BasicDataSource ds = (BasicDataSource)ctx.getAttribute("dataSource");
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rset = null;
		String sql = "select resident_number, name, address, phone_number, salary from customer where resident_number=?";
		try {
			conn = ds.getConnection();
			pstmt = conn.prepareStatement(sql);
			pstmt.setString(1, id);
			rset = pstmt.executeQuery();
			if(rset.next()) {
				out.println("주민번호 : " + rset.getString(1)+"<br>");
				out.println("이름 : " + rset.getString(2)+"<br>");
				out.println("주소 : " + rset.getString(3)+"<br>");
				out.println("전화번호 : " + rset.getString(4)+"<br>");
				out.println("연봉 : " + rset.getInt(5)+"<br>");
				
			}else {
				out.println("조회하려는 고객이 없습니다.");
			}
			} catch (SQLException e) {
				e.printStackTrace();
				out.println("오류가 발생했습니다."+e.getMessage());
				
		}finally{
			//연결닫기
		}
		
		
	}

}
